const mongoose = require("mongoose");

const TaskNotificationSchema = new mongoose.Schema(
  {
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true,
    },
    task: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Task",
      required: true,
    },
    type: {
      type: String,
      enum: [
        "task_assigned",
        "task_updated",
        "task_completed",
        "task_due_soon",
        "task_overdue",
        "comment_added",
        "status_changed",
      ],
      required: true,
    },
    title: {
      type: String,
      required: true,
      trim: true,
    },
    message: {
      type: String,
      required: true,
      trim: true,
    },
    isRead: {
      type: Boolean,
      default: false,
    },
    readAt: {
      type: Date,
      default: null,
    },
    // 通知相关的额外数据
    metadata: {
      type: mongoose.Schema.Types.Mixed,
      default: {},
    },
  },
  { timestamps: true }
);

// 索引优化
TaskNotificationSchema.index({ user: 1, isRead: 1, createdAt: -1 });
TaskNotificationSchema.index({ task: 1 });
TaskNotificationSchema.index({ type: 1 });

const TaskNotification = mongoose.model(
  "TaskNotification",
  TaskNotificationSchema
);

module.exports = TaskNotification;
